var websocket = null;
var address = "192.168.18.102:7071";
//下面ws/ 后面是用户token
var url =
  "ws://" +
  address +
  "/ws/eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MjcwMzYwNzAsInVzZXJOdW1iZXIiOiIwMTAxMDAwNjMyMSIsInVzZXJJZCI6IjEzMzRmNWQyMjQyZDQ1MDhiMmYxNGJhOTI2OWI2MDFhIiwiaWF0IjoxNjI1NzIyMDcwLCJyb2wiOiJyb2wifQ.cxFjh1hiz4lhW7o08ZijP5uULJwTltFbJsMyxScb4_4";

if ("WebSocket" in window) {
  alert("您的浏览器支持 WebSocket!");
  var websocket = new WebSocket("ws://172.20.41.71:7071/messages", [token]);

  // 打开一个 web socket
  websocket.onopen = function (event) {
    console.log("建立连接");
  };
  websocket.onclose = function (event) {
    // 关闭 websocket
    alert("连接已关闭...");
  };

  websocket.onmessage = function (event) {
    console.log("收到消息:" + event.data);
  };
  websocket.onerror = function (event) {
    alert("websocket通信发生错误!");
  };
  window.onbeforeunload = function () {
    websocket.close();
  };
} else {
  // 浏览器不支持 WebSocket
  alert("您的浏览器不支持 WebSocket!");
}
